package org.ruoyi.common.dhuman.backup;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.ruoyi.common.dhuman.utils.LocalSubtitleGenerator;
import org.ruoyi.common.dhuman.utils.v1.TTS;
import org.ruoyi.common.dhuman.utils.v1.vo.TTSVo;
import org.ruoyi.common.dhuman.utils.v1.Video;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.yaml.snakeyaml.Yaml;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

@SpringBootApplication
public class DemoTtsApplication {
	public static void main(String[] args) {
		String ttsAppid = StringUtils.EMPTY;
		String ttsAppSecret = StringUtils.EMPTY;
		String ttsEndpoint = StringUtils.EMPTY;
		String ttsWs = StringUtils.EMPTY;
		String videoAppid = StringUtils.EMPTY;
		String videoAppSecret = StringUtils.EMPTY;
		String videoEndpointCreate = StringUtils.EMPTY;
		String videoEndpointStatus = StringUtils.EMPTY;
		// 从 config.ini 中加载 appid 和 appSecret
//		try {
//			Wini wini = new Wini(new File("config.ini"));
//			Profile.Section section = wini.get("KEY");
//			ttsAppid = section.get("appid");
//			ttsAppSecret = section.get("appsecret");
//			ttsEndpoint = section.get("endpoint");
//		} catch (IOException e) {
//			e.printStackTrace();
//			return;
//		}

		// 从 application-dev.yml 中加载 appid 和 appSecret
		Yaml yaml = new Yaml();
		try (InputStream inputStream = new FileInputStream("ruoyi-admin/src/main/resources/application-dev.yml")) {
			Iterable<Object> loadedDocuments = yaml.loadAll(inputStream);
			for (Object doc : loadedDocuments) {
				Map<String, Object> map = (Map) doc;
				if (map.containsKey("dhuman")) {
					Map<String, Object> dhuman = (Map<String, Object>) map.get("dhuman");
					ttsAppid = dhuman.get("ttsAppid").toString();
					ttsAppSecret = dhuman.get("ttsAppsecret").toString();
					ttsEndpoint = dhuman.get("ttsEndpoint").toString();
					ttsWs = dhuman.get("ttsWs").toString();

					videoAppid = dhuman.get("videoAppid").toString();
					videoAppSecret = dhuman.get("videoAappsecret").toString();
					videoEndpointCreate = dhuman.get("videoEndpointCreate").toString();
					videoEndpointStatus = dhuman.get("videoEndpointStatus").toString();

					break;
				}
			}
			// 处理数据

		} catch (IOException e) {
			e.printStackTrace();
			return;
		}

//		String text = "寒露，秋季的第五个节气，露水在凉意中凝成寒意，故名“寒露”。\n" +
//			"\n" +
//			"此时，北方的秋意已深，天高云淡，鸿雁南迁。清晨的草木间，露水晶莹，触手生凉，预示着气候从凉爽向寒冷的过渡。南方虽仍存暑热余韵，但早晚的风里，已能嗅到清冷的气息。\n" +
//			"\n" +
//			"寒露时节，民间有赏菊、登高、吃芝麻的习俗，既是顺应自然，也为身体积蓄能量以御冬寒。\n" +
//			"\n" +
//			"寒露至，秋已深，人间向暖，且待冬来。";
//		String voiceName = "xiaoya";
//		String audio_url = tts(ttsAppid, ttsAppSecret, ttsEndpoint, text, voiceName	);
//		System.out.println(audio_url);
//		String video_id = createVideo(videoAppid, videoAppSecret, videoEndpointCreate, videoEndpointStatus, audio_url, "44512", null, null);
		String video_id = "5093";
		String video_status = queryVideo(videoAppid, videoAppSecret, videoEndpointCreate, videoEndpointStatus, video_id);

//        WS ws = new WS();
//        ws.loadConf();
//        ws.doTTS("欢迎使用深声科技语音合成服务");
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
	}

	public static String tts(String appid, String appSecret, String endpoint, String text, String voiceName) {
		TTS tts = new TTS(appid, appSecret, endpoint);
		TTSVo requestVO = new TTSVo();
		requestVO.setText(text);
		requestVO.setVoice_name(voiceName);
		requestVO.setWith_caption(1);
		requestVO.setCaption_type("tlx");
		String response = tts.doTTS(requestVO);
		String audio_url = "";

		try {
			ObjectMapper mapper = new ObjectMapper();
			JsonNode jsonNode = mapper.readTree(response);

			if (jsonNode.has("error_code")) {
				System.err.println(jsonNode.get("error_msg"));
				return null;
			}

			int code = jsonNode.get("code").asInt();
			if (code == 0) {
				audio_url = jsonNode.get("data").get("tts").get("url").asText();
				System.out.println("获取到的音频URL: " + audio_url);
			} else {
				System.out.println("请求失败，错误码: " + code);
				System.out.println("错误信息: " + jsonNode.get("message").asText());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return audio_url;
	}

	public static String createVideo(
		String videoAppid,
		String videoAppSecret,
		String videoEndpointCreate,
		String videoEndpointStatus,
		String audio_url, String model_id, String callback_url, String video_info) {

		Video video = new Video(videoAppid, videoAppSecret, videoEndpointCreate, videoEndpointStatus);
		String response = video.createVideo(audio_url, model_id, callback_url, video_info);
		String video_id = "";

		try {
			ObjectMapper mapper = new ObjectMapper();
			JsonNode jsonNode = mapper.readTree(response);

			int code = jsonNode.get("code").asInt();
			if (code == 0) {
				video_id = jsonNode.get("data").get("video_id").asText();
				System.out.println("获取到的视频ID: " + video_id);
			} else {
				System.out.println("请求失败，错误码: " + code);
				System.out.println("错误信息: " + jsonNode.get("message").asText());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return model_id;
	}

	public static String queryVideo(
		String videoAppid,
		String videoAppSecret,
		String videoEndpointCreate,
		String videoEndpointStatus,
		String video_id) {
		Video video = new Video(videoAppid, videoAppSecret, videoEndpointCreate, videoEndpointStatus);
		String response = video.queryVideo(video_id);

		try {
			ObjectMapper mapper = new ObjectMapper();
			JsonNode jsonNode = mapper.readTree(response);
			String status = "";
			int code = jsonNode.get("code").asInt();
			if (code == 0) {
				status = jsonNode.get("data").get("status").asText();
				System.out.println("获取到的视频状态: " + status);
			} else {
				System.out.println("请求失败，错误码: " + code);
				System.out.println("错误信息: " + jsonNode.get("message").asText());
			}
			return status;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
}
